package com.dcrzl.shcz.module.info.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.dcrzl.shcz.common.entity.info.Gov;
import com.dcrzl.shcz.common.entity.sys.SysRoleGov;
import com.dcrzl.shcz.common.mapper.info.GovMapper;
import com.dcrzl.shcz.common.service.impl.BaseServiceImpl;
import com.dcrzl.shcz.common.utils.PageInfo;
import com.dcrzl.shcz.common.utils.Result;
import com.dcrzl.shcz.common.utils.SysConstant;
import com.dcrzl.shcz.module.info.service.GovService;
import com.dcrzl.shcz.module.sys.service.SysRoleGovService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service("govService")
public class GovServiceImpl extends BaseServiceImpl<GovMapper, Gov> implements GovService {

    @Autowired
    private SysRoleGovService sysRoleGovService;

    private  LambdaQueryWrapper<Gov> commonWrapper(Gov gov) {
        LambdaQueryWrapper<Gov> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Objects.nonNull(gov.getId()), Gov::getId, gov.getId());
        queryWrapper.like(Objects.nonNull(gov.getName()), Gov::getName, gov.getName());
        queryWrapper.eq(Objects.nonNull(gov.getStatus()), Gov::getStatus, gov.getStatus());
        queryWrapper.in(!CollectionUtils.isEmpty(gov.getIdList()), Gov::getId, gov.getIdList());
        return queryWrapper;
    }

    @Override
    public Result<Gov> selectOne(Gov gov) {
        LambdaQueryWrapper<Gov> wrapper = this.commonWrapper(gov);
        return super.selectOne(wrapper);
    }

    @Override
    public Result<List<Gov>> selectList(Gov gov) {
        LambdaQueryWrapper<Gov> wrapper = this.commonWrapper(gov);
        return super.selectList(wrapper);
    }

    @Override
    public PageInfo<Gov> selectPage(Gov gov) {
        LambdaQueryWrapper<Gov> wrapper = this.commonWrapper(gov);
        return super.selectPage(gov, wrapper);
    }

    @Override
    public Result<List<Gov>> selectUList(Gov gov) {
        Gov gov1 = new Gov().setStatus(SysConstant.DATA_NORMAL_STATUS);
        if (gov.getRoleId() != 1L) {//超级管理员
            Result<List<SysRoleGov>> listResult = sysRoleGovService.selectList(new SysRoleGov().setRoleId(gov.getRoleId()));
            if (listResult.isSuccess()) {
                List<SysRoleGov> sysRoleGovList = listResult.getData();
                List<Long> govIdList = new ArrayList<>();
                sysRoleGovList.forEach(sysRoleGov1 -> {
                    govIdList.add(sysRoleGov1.getGovId());
                });
                gov1.setIdList(govIdList);
            }
        }
        return this.selectList(gov1);
    }

    @Override
    public Result<Integer> removeById(Serializable id) {
        Gov gov = new Gov().setStatus(SysConstant.DATA_DELETE_STATUS)
                .setId((Long) id);
        return this.updateById(gov);
    }

}